#!/bin/sh

WORK="${TMPDIR:-/tmp}/nvm_download_artifact_cache.$$"

cleanup () {
  command rm -rf "${WORK}"
  unset -f cleanup die download_offline nvm_get_mirror nvm_binary_available nvm_get_download_slug nvm_get_artifact_compression nvm_cache_dir nvm_get_checksum nvm_version_greater_than_or_equal_to nvm_download nvm_compute_checksum nvm_compare_checksum nvm_get_checksum_binary
}
die () { echo "$@" ; cleanup ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

\. ../../common.sh

TEST_SLUG='node-v20.0.0-linux-x64'
TEST_CACHE_DIR="${WORK}/.cache/bin/${TEST_SLUG}"
TEST_TARBALL="${TEST_CACHE_DIR}/${TEST_SLUG}.tar.gz"

nvm_get_mirror() { nvm_echo 'http://example.com'; }
nvm_binary_available() { return 0; }
nvm_get_download_slug() { nvm_echo "${TEST_SLUG}"; }
nvm_get_artifact_compression() { nvm_echo 'tar.gz'; }
nvm_cache_dir() { nvm_echo "${WORK}/.cache"; }
nvm_get_checksum() { nvm_echo 'fake_checksum'; }
nvm_version_greater_than_or_equal_to() { return 0; }

# appends "downloaded" to the -o target, and records whether that target is the final cache name
nvm_download() {
  while [ "$#" -gt 0 ]; do
    if [ "$1" = '-o' ]; then
      if [ "$2" = "${TEST_TARBALL}" ]; then
        command touch "${WORK}/downloaded-to-final-name"
      fi
      nvm_echo 'downloaded' >> "$2"
      return 0
    fi
    shift
  done
  return 1
}

download_offline() {
  NVM_OFFLINE=1
  nvm_download_artifact node binary std v20.0.0
}

command mkdir -p "${WORK}"

# a download with no expected checksum to compare against must not be left in the cache
# (this uses the real `nvm_compare_checksum`, so it runs before that is mocked)
nvm_get_checksum() { nvm_echo ''; }
try nvm_download_artifact node binary std v20.0.0
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "empty checksum: expected exit code 6, got ${CAPTURED_EXIT_CODE}"
[ ! -e "${TEST_CACHE_DIR}" ] || die "empty checksum: expected ${TEST_CACHE_DIR} to be removed"
try download_offline
[ "${CAPTURED_EXIT_CODE}" = '4' ] || die "empty checksum: expected --offline to find no cached archive (exit code 4), got ${CAPTURED_EXIT_CODE}"
nvm_get_checksum() { nvm_echo 'fake_checksum'; }

# a download whose checksum cannot be computed has not been verified, so it must not be accepted either
# (this still uses the real `nvm_compare_checksum`, so it runs before that is mocked)
nvm_compute_checksum() { nvm_echo ''; }
try nvm_download_artifact node binary std v20.0.0
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "unverifiable checksum: expected exit code 6, got ${CAPTURED_EXIT_CODE}"
[ ! -e "${TEST_CACHE_DIR}" ] || die "unverifiable checksum: expected ${TEST_CACHE_DIR} to be removed"
try download_offline
[ "${CAPTURED_EXIT_CODE}" = '4' ] || die "unverifiable checksum: expected --offline to find no cached archive (exit code 4), got ${CAPTURED_EXIT_CODE}"

# nor may a cached archive whose checksum cannot be computed be used
command mkdir -p "${TEST_CACHE_DIR}"
nvm_echo 'cached' > "${TEST_TARBALL}"
try nvm_download_artifact node binary std v20.0.0
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "unverifiable cached archive: expected exit code 6, got ${CAPTURED_EXIT_CODE}"
[ ! -e "${TEST_CACHE_DIR}" ] || die "unverifiable cached archive: expected ${TEST_CACHE_DIR} to be removed"

# a download that fails its checksum must not be left in the cache either
nvm_compare_checksum() { return 1; }
try nvm_download_artifact node binary std v20.0.0
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "checksum mismatch: expected exit code 6, got ${CAPTURED_EXIT_CODE}"
[ ! -e "${TEST_CACHE_DIR}" ] || die "checksum mismatch: expected ${TEST_CACHE_DIR} to be removed"
try download_offline
[ "${CAPTURED_EXIT_CODE}" = '4' ] || die "checksum mismatch: expected --offline to find no cached archive (exit code 4), got ${CAPTURED_EXIT_CODE}"

# a successful download is moved into the cache only once it is verified, replacing any leftover partial download
nvm_compare_checksum() { return 0; }
command mkdir -p "${TEST_CACHE_DIR}"
nvm_echo 'partial' > "${TEST_TARBALL}.unverified"
command mkdir -p "${TEST_TARBALL}.unverified.abc123"
nvm_echo 'partial' > "${TEST_TARBALL}.unverified.abc123/${TEST_SLUG}.tar.gz"
try nvm_download_artifact node binary std v20.0.0
[ "${CAPTURED_EXIT_CODE}" = '0' ] || die "verified download: expected exit code 0, got ${CAPTURED_EXIT_CODE}"
[ "${CAPTURED_STDOUT}" = "${TEST_TARBALL}" ] || die "verified download: expected >${TEST_TARBALL}<, got >${CAPTURED_STDOUT}<"
[ "$(command cat "${TEST_TARBALL}")" = 'downloaded' ] || die "verified download: expected the cached archive to hold only the new download, got >$(command cat "${TEST_TARBALL}")<"
[ "$(command ls "${TEST_CACHE_DIR}")" = "$(nvm_echo 'files'; nvm_echo "${TEST_SLUG}.tar.gz")" ] || die "verified download: expected no unverified download to remain in the cache, got >$(command ls "${TEST_CACHE_DIR}")<"
[ ! -e "${WORK}/downloaded-to-final-name" ] || die 'an unverified download was written to the final cache name, where an interrupted download would be left for --offline'

# without any checksum tool, nothing is downloaded and the verified cache is left alone
nvm_get_checksum_binary() { return 1; }
try nvm_download_artifact node binary std v20.0.0
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "no checksum tool: expected exit code 6, got ${CAPTURED_EXIT_CODE}"
[ "$(command cat "${TEST_TARBALL}")" = 'downloaded' ] || die "no checksum tool: expected the cached archive to be left alone, got >$(command cat "${TEST_TARBALL}")<"
[ "$(command ls "${TEST_CACHE_DIR}")" = "$(nvm_echo 'files'; nvm_echo "${TEST_SLUG}.tar.gz")" ] || die "no checksum tool: expected nothing to be downloaded, got >$(command ls "${TEST_CACHE_DIR}")<"
nvm_get_checksum_binary() { nvm_echo 'sha256sum'; }

# a downloader left running by an earlier install, which opens its output only once a later install is downloading
# (as curl does when the response body is late), must not be able to write to the archive that the later install verified
command rm -rf "${WORK}/.cache"
nvm_compare_checksum() { [ "$(command cat "$1")" = 'published' ]; }
nvm_download() {
  while [ "$#" -gt 1 ] && [ "$1" != '-o' ]; do
    shift
  done
  if [ ! -e "${WORK}/first-download" ]; then
    nvm_echo "$2" > "${WORK}/first-download"
    nvm_download_artifact node binary std v20.0.0 > "${WORK}/second-install" 2>/dev/null
    nvm_echo "$?" >> "${WORK}/second-install"
    if [ -e "${WORK}/late-open" ]; then
      nvm_echo 'tampered' >&9
      exec 9>&-
    fi
    command cat "${TEST_TARBALL}" > "${WORK}/cached-after-late-write"
  else
    # a failed `exec` redirection would end the shell; where the path is gone, curl would fail to open it
    if [ -d "$(command dirname "$(command cat "${WORK}/first-download")")" ]; then
      exec 9> "$(command cat "${WORK}/first-download")"
      command touch "${WORK}/late-open"
    fi
    nvm_echo 'published' > "$2"
  fi
}
try nvm_download_artifact node binary std v20.0.0
[ "$(command cat "${WORK}/second-install")" = "$(nvm_echo "${TEST_TARBALL}"; nvm_echo 0)" ] || die "late write: expected the second install to cache its download, got >$(command cat "${WORK}/second-install")<"
[ "$(command cat "${WORK}/cached-after-late-write")" = 'published' ] || die "late write: expected the verified archive in the cache to stay as it was, got >$(command cat "${WORK}/cached-after-late-write")<"
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "late write: expected the first install to reject its own download (exit code 6), got ${CAPTURED_EXIT_CODE}"

cleanup
